//景区舒适度分析 查询景区舒适度数据
srvMap.add('queryRegionComfortList','','front/sh/sceneMonitor!execute?uid=queryRegionComfortList');
//查询景区舒适度信息
srvMap.add('queryComfortLevel', '','front/sh/sceneMonitor!execute?uid=queryComfortLevel');
//查询景区图片信息
srvMap.add('queryRegionImage', '','front/sh/sceneMonitor!execute?uid=queryRegionImage');

//定义一个分页数据
var initPage;
var selectColor = "#D3E5F2"; //选中底色
var whiteColor = "#FFFFFF";
var selected_region;
var selected_region_n;
var pageChange=0;


$(function() {
	//初始化查询条件信息
	initPage();
	//初始化左侧区域展示
	
	var cityId = $('#citySelect span li').attr("d_value")|| "";
	var regionType = $('#regionTypleSelect span li').attr("d_value")|| "";

	showRegionComfortList(cityId,regionType);
	
	//根据景区ID查询舒适度信息
	initComfort(selected_region,selected_region_n);
	//根据景区ID查询图片信息
	queryRegionImage(selected_region);
	
	//绑定查询事件
	$(".select_button").click(function(){
		var cityId = $('#citySelect span li').attr("d_value")|| "";
		var regionType = $('#regionTypleSelect span li').attr("d_value")|| "";
		
		showRegionComfortList(cityId,regionType);
	});
	
	//景区搜索
	$("#searchInput").keyup(function(){
		searchRegions();
	});	
	
	$("#searchInput").bind("keydown",function(e){
        // 兼容FF和IE和Opera    
	var theEvent = e || window.event;    
	var code = theEvent.keyCode || theEvent.which || theEvent.charCode;
	if (code == 13) {    
	    //回车执行查询
		searchRegions();
        }    
	});
	
});


/**
 * 初始化页面
 * @return {[type]} [description]
 */
function initPage() {	
	Util.loadUpInfo.initCityList();
	Util.loadUpInfo.initRegionTypleList();
	
}

/**
 * 查询景区舒适度排名信息
 */
function showRegionComfortList(city_id,region_type) {
	var par = "&cityId="+city_id+"&regionType="+region_type;
	Util.ajax.postJsonSync(srvMap.get('queryRegionComfortList'), par, function(json,status){
		if(status){
			
			//展示景区舒适度列表
			showLeftList(json.object);
		}else{
			 alert('错误','数据异常！');   
		}
	});
}
/**
 * 展示景区舒适度列表
 */
function showLeftList(data){
	//创建一个分页
	var group = 0;	
	
	//清空左侧列表
	$("#wisdon_left_list_ul").empty();
	//显示左侧列表-无数据时
	if (data.length == 0) {
		$("#wisdon_left_list_ul").append('<div style="color:#F00;font-size:14px;text-align:center;margin-top:10px;">暂无景区舒适度排行信息！</div>');
	}
	//显示左侧列表-有数据时
	var totalContent="";
	for (var i = 0; i < data.length; i++) {
		var update_time = data[i].op_time;
		var region_id = data[i].region_id;
		var region_name = data[i].region_name;
		var region_name_sub="";
		if(region_name.length<=5){region_name_sub=region_name;}
		else {region_name_sub=region_name.substr(0,5)+"…";}
		var comfortable_level = data[i].comfortable_level;
		if(comfortable_level>10){//舒适度超过10时，置为10
			comfortable_level=10;
		}
		//舒适度值0-10,求百分比
		var comfort_pct=comfortable_level+"0%";
		if (i % 10 == 0) {
			group++;
		}
		if(i==0){
			var content='<li class="sorting" id="$3" name="$1" title="$1" group="$5" onclick="showInfo(this)"> <p class="l1"><input class="s1" value="$0"></p> <p class="l2" id="l2_name">$6</p> <div class="div_co"> <div class="div_per div_per1" style="width:$4">$2</div> </div></li>';
		}
		if(i==1){
			var content='<li class="sorting" id="$3" name="$1" title="$1" group="$5" onclick="showInfo(this)"> <p class="l1"><input class="s2" value="$0"></p> <p class="l2">$6</p> <div class="div_co"> <div class="div_per div_per2" style="width:$4">$2</div> </div></li>';
		}
		if(i==2){
			var content='<li class="sorting" id="$3" name="$1" title="$1" group="$5" onclick="showInfo(this)"> <p class="l1"><input class="s3" value="$0"></p> <p class="l2">$6</p> <div class="div_co"> <div class="div_per div_per3" style="width:$4">$2</div> </div></li>';
		}
		if(i>=3){
			var content='<li class="sorting" id="$3" name="$1" title="$1" group="$5" onclick="showInfo(this)"> <p class="l1"><input class="s4" value="$0"></p> <p class="l2">$6</p> <div class="div_co"> <div class="div_per div_per4" style="width:$4">$2</div> </div></li>';
		}
        content=content.replace(/\$0/g, i+1);
        content=content.replace(/\$1/g, region_name);
        content=content.replace(/\$2/g, comfortable_level);
        content=content.replace(/\$3/g, region_id);
        content=content.replace(/\$4/g, comfort_pct);
        content=content.replace(/\$5/g, group);
        content=content.replace(/\$6/g, region_name_sub);
        
        
        //var content='<dd style="background:url(assets/css/images/crowd/purple'+crowStype+'_hover.png) no-repeat -3px 8px; padding-left: 45px;padding-bottom:8px;padding-top: 0px; margin-left: 0px; margin-right: 0px; border-bottom: 1px solid #f2f6f8; cursor: pointer;" region_id="' + crowId+'" name="'+crowName+'" class="region-li" group="' + group + '" search="0"><h3>'+crowName+'</h3><p>分布人员总数<span>'+newGroupUCnt + '/' + crowNum+'</span></p></dd>';
		totalContent+=content;
	}

	var newContent=$(totalContent+'<div class="tcdPageCode" id="tcdPageCode" style="margin-left:100px; margin-top:25px; margin-bottom:16px;"></div>');
	
	$("#wisdon_left_list_ul").append(newContent);
	initPage = group;
	
	//设置分页
	$("#tcdPageCode").createPage({
		pageCount: group,
		current: 1,
		backFn: function(p) {
			showPage(p);
		}
	});

	//默认加载时展示第一页数据
	showPage(1);
}

/**
 * 绘制选中底色
 */
function setSelectColor(regionId) {
	$(".sorting").each(function(i) {
		if ($(this).attr("id") == regionId) {
			$(this).css("background-color", selectColor);							
		}
		else {
			$(this).css("background-color", whiteColor);
		}
	});
	selected_region=regionId;
	selectde_region_n=$("#"+regionId).attr("name");
	//var regionName=$("#"+regionId).attr("name");
	//根据景区ID查询舒适度信息
	//initComfort(regionId,regionName);
	//根据景区ID查询图片信息
	//queryRegionImage(regionId);
}

//搜素功能实现
var searchRegions = function() {
	var keyValue = $("#searchInput").val();
	keyValue = $.trim(keyValue);
	//var areaValue = $("#region_id").val();
	
	$(".sorting").hide();
	//if (areaValue == DEPLOY_PROVINCE_ID) {
	//	areaValue = "";
	//}
	if (keyValue == "") {
		showPage(1);
		//$(".showNoneInfo").hide();
		$(".tcdPageCode").createPage({
			pageCount: initPage,
			current: 1,
			backFn: function(p) {
				showPage(p);
			}
		});
	} else {
		//更新左侧列表
		$(".sorting").hide();
		var searchRes = 0;
		$(".sorting").each(function(i) {
			var id = this.id;
			var name = $(this).attr("name");
			if ((name.indexOf(keyValue) >= 0 || id.indexOf(keyValue) >= 0)) {
				searchRes++;
				var searchPage = Math.ceil(searchRes / 10);
				$(this).show();
				$(this).attr("search", searchPage);
				//$(".showNoneInfo").hide();
			} else {
				$(this).attr("search", "0");
				$(this).hide();
			}
		});
		var resPage = Math.ceil(searchRes / 10);
		$(".tcdPageCode").createPage({
			pageCount: resPage,
			current: 1,
			backFn: function(p) {
				showPage2(p);
			}
		});
		showPage2(1);
	}	
};

//初始化数据展示
function showPage(pageNum) {
	//记录左侧翻页次数
	pageChange=pageChange+1;
	var id_="";
	var name_="";
	var index = 0;
	$("#wisdon_left_list_ul").show();
	$(".showNoneInfo").hide();
	$(".sorting").each(function(i) {
		if ($(this).attr("group") == pageNum) {
			$(this).show();
			if (index == 0) {
				var regionId = $(this).attr("id");
				//groupId = regionId;
				setSelectColor(regionId); //改变底色
				id_=$(this).attr("id");
				name_=$(this).attr("name");
			}
			index++;
		} else {
			$(this).hide();
		}
	});

	if(pageChange!=1){
		//根据景区ID查询舒适度信息
		initComfort(id_,name_);
		//根据景区ID查询图片信息
		queryRegionImage(id_);
	}
	if(index==0){
		$("#wisdon_left_list_ul").hide();
		$(".showNoneInfo").show();
	}
}

//搜索数据展示
function showPage2(pageNum) {
	var index = 0;
	$("#wisdon_left_list_ul").show();
	$(".showNoneInfo").hide();
	$(".sorting").each(function(i) {
		if ($(this).attr("search") == pageNum) {
			$(this).show();
			if (index == 0) {
				var regionId = $(this).attr("id");
				//groupId = regionId;
				setSelectColor(regionId); //改变底色
				
				//根据景区ID查询舒适度信息
				initComfort($(this).attr("id"),$(this).attr("name"));
				//根据景区ID查询图片信息
				queryRegionImage($(this).attr("id"));
			}
			index++;
		} else {
			$(this).hide();
		}
	});
	if(index==0){
		$("#wisdon_left_list_ul").hide();
		$(".showNoneInfo").show();
	}
}
//点击左侧列表，展示景区详细指数
function showInfo(obj){
	//点击，选中，改变底色
	setSelectColor($(obj).attr("id"));
	
	//根据景区ID查询舒适度信息
	initComfort($(obj).attr("id"),$(obj).attr("name"));
	//根据景区ID查询图片信息
	queryRegionImage($(obj).attr("id"));
	
}

//初始化景区舒适度信息
function initComfort(region_Id,region_Name){
	var par = "&regionId="+region_Id;
	Util.ajax.postJsonSync(srvMap.get('queryComfortLevel'),par,function(json,status){
		if(status){
			var comfortable_level=json.object[0].comfortable_level;
			var op_time=json.object[0].op_time;
			var update_t=op_time.substr(0,4)+"-"+op_time.substr(4,2)+"-"+op_time.substr(6,2)+" "+op_time.substr(8,3)+":00";
			if(region_Name == undefined){
				region_Name=$("#l2_name").text();
			}
			
			//景区名称
			$("#region_name").text(region_Name);
			//数据刷新时间
			$("#update_time").text("数据刷新时间："+update_t);
			//舒适度指数
			$("#text_two").text(comfortable_level);
			//舒适度等级
			$("#text_two_desc").text((json.object[0].comfortable_level_desc==""?"暂无描述":json.object[0].comfortable_level_desc));
			//初始化舒适度星级展示
			initStarShow(comfortable_level);
			//温度指数星级展示
			initStarShow2(json.object[0].temper_level,"wendu_");
			$("#wendu").text(json.object[0].temper_level);
			//污染指数星级展示
			initStarShow2(json.object[0].contamin_level,"wuran_");
			$("#wuran").text(json.object[0].contamin_level);
			//天气指数星级展示
			initStarShow2(json.object[0].weather_level,"tianqi_");
			$("#tianqi").text(json.object[0].weather_level);
			//旅游舒适度指数星级展示
			initStarShow2(json.object[0].tourism_com_level,"shushi_");
			$("#shushi").text(json.object[0].tourism_com_level);
			//游客密度指数星级展示
			initStarShow2(json.object[0].density_level,"midu_");
			$("#midu").text(json.object[0].density_level);
		}else{
			 alert('查询舒适度信息出错！');   
		}
	});
}

/**
 * 舒适度星级展示
 */
function initStarShow(cnt){
	//初始化舒适度等级展示为灰色
	$(".text_three ul li").removeClass("stra_yellow");
	$(".text_three ul li").removeClass("star_half");
	$(".text_three ul li").addClass("star_gray");
	
	//舒适度指数除2取整得到中间值
	var mid_cnt=cnt/2;
	//小于等于中间值的点亮
	for(var i=1;i<=mid_cnt;i++){
		$("#star_"+i).removeClass("star_gray");
		$("#star_"+i).addClass("stra_yellow");
	}
	//舒适度指数对2取模,对不能整除的点亮半星
	if(cnt%2 != 0){
		var half_index=parseInt(mid_cnt)+1;
		$("#star_"+half_index).removeClass("star_gray");
		$("#star_"+half_index).addClass("star_half");
	}	
}

/**
 * 其他指数星级展示
 */
function initStarShow2(cnt,css_Class){
	//初始化舒适度等级展示为灰色
	for(var j=1;j<=5;j++){
		$("#"+css_Class+j).removeClass("stra_one");
		$("#"+css_Class+j).removeClass("stra_half");
		$("#"+css_Class+j).addClass("stra_none");
	}
	
	//舒适度指数除2取整得到中间值
	var mid_cnt=cnt/2;
	//小于等于中间值的点亮
	for(var i=1;i<=mid_cnt;i++){
		$("#"+css_Class+i).removeClass("stra_none");
		$("#"+css_Class+i).addClass("stra_one");
	}
	//舒适度指数对2取模,对不能整除的点亮半星
	if(cnt%2 != 0){
		var half_index=parseInt(mid_cnt)+1;
		$("#"+css_Class+half_index).removeClass("stra_none");
		$("#"+css_Class+half_index).addClass("stra_half");
	}	
}

//初始化景区舒适度信息
function queryRegionImage(region_Id){
	//$(".picture").children().attr("src","/DMC-IDX-WEB/zhly/assets/css/images/region_dft.png");
	
	var par = "&regionId="+region_Id;
	Util.ajax.postJsonSync(srvMap.get('queryRegionImage'),par,function(json,status){
		if(status){
		var totalContent="";			
		var imgArr=json.object;
		//最后一张图片为默认的“暂无图片”
		for(var k=0;k<imgArr.length-1;k++){
			//$("#img_"+k).attr("src","" + imgArr[k].IMG_BASE64_CODE);
			var content='<li class="picture" id="picture_$1"><img class="photo_show" src="$2" /></li>';
			content=content.replace(/\$1/g, k);
			content=content.replace(/\$2/g, "data:image/jpeg;base64,"+imgArr[k].IMG_BASE64_CODE);
			totalContent+=content;
		}
		//当景区未配置图片时，显示“暂无图片”
		if(imgArr.length==1){
			var defaultContent='<li class="picture" id="picture_0"><img class="photo_show" src="$0" /></li>'
				+'<li class="picture" id="picture_1"><img class="photo_show" src="$0" /></li>'
				+'<li class="picture" id="picture_2"><img class="photo_show" src="$0" /></li>';
			defaultContent=defaultContent.replace(/\$0/g, "data:image/jpeg;base64,"+imgArr[0].IMG_BASE64_CODE);
			totalContent+=defaultContent;
		}//当景区配置1图片时，2、3显示“暂无图片”
		if(imgArr.length==2){
			var defaultContent='<li class="picture" id="picture_1"><img class="photo_show" src="$0" /></li>'
				+'<li class="picture" id="picture_2"><img class="photo_show" src="$0" /></li>';
			defaultContent=defaultContent.replace(/\$0/g, "data:image/jpeg;base64,"+imgArr[1].IMG_BASE64_CODE);
			totalContent+=defaultContent;
		}//当景区配置2图片时，3显示“暂无图片”
		if(imgArr.length==3){
			var defaultContent='<li class="picture" id="picture_2"><img class="photo_show" src="$0" /></li>';
			defaultContent=defaultContent.replace(/\$0/g, "data:image/jpeg;base64,"+imgArr[2].IMG_BASE64_CODE);
			totalContent+=defaultContent;
		}
		
		//清空图片列表
		$("#region_image_list").empty();
		var pre_content='<li class="toward_left" id="toward_left" onclick="towardLeft()"></li>';
		var next_content='<li class="toward_right" id="toward_right" onclick="towardRight()"></li>';
		var show_content =pre_content+totalContent+next_content;
		$("#region_image_list").append(show_content);
		
		//初始化展示前3张，后面的屏蔽
		if(imgArr.length>3){
			for(var j=3;j<imgArr.length;j++){
				$("#picture_"+j).css('display','none'); 
			}			
		}
		
		}else{
			 alert('查询景区图片信息出错！');
		}
	});
}

function towardLeft(){
	var img_len=$('.picture').length;
	var first_photo_num=0;
	
	$('.picture').each(function(){
		if($(this).css('display') != 'none')
        {	//获取第一个显示图片
			first_photo_num=parseInt($(this).attr("id").substr(8,$(this).attr("id").length-8));
    		return false;//退出当前循环
        }
    });
	if(first_photo_num!=0){
		var plus1=first_photo_num-1;
		var plus2=first_photo_num+1;
		var plus3=first_photo_num+2;
		$("#picture_"+plus1).show();
		$("#picture_"+first_photo_num).show();
		$("#picture_"+plus2).show();
		$("#picture_"+plus3).hide();
	}
		
}

function towardRight(){
	var img_len=$('.picture').length;
	var first_photo_num=0;
	
	$('.picture').each(function(){
		if($(this).css('display') != 'none')
        {	//获取第一个显示图片
			first_photo_num=parseInt($(this).attr("id").substr(8,$(this).attr("id").length-8));
    		return false;//退出当前循环
        }
    });
if(first_photo_num>=0 && (first_photo_num+3)<=img_len){
		var plus1=first_photo_num+1;
		var plus2=first_photo_num+2;
		var plus3=first_photo_num+3;
		if((first_photo_num+3)<img_len){
			$("#picture_"+first_photo_num).hide();			
		}
		$("#picture_"+plus1).show();
		$("#picture_"+plus2).show();
		$("#picture_"+plus3).show();
	}
	
}